package com.example.serviceandroidjava.aidlClient;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.util.Log;

import com.example.serviceandroidjava.IRemoteService;
import com.example.serviceandroidjava.R;

public class AidlClientActivity extends AppCompatActivity {
    private IRemoteService iRemoteService;
    private final String TAG = "AidlClientActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aidl_client);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent();
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unbindService(mConnection);
    }

    ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            iRemoteService = IRemoteService.Stub.asInterface(iBinder);
            try {
                int pid = iRemoteService.getPid();
                int currentPid = Process.myPid();
                Log.d(TAG, "currentPid: " + currentPid + ", remotePid: " + pid);
                iRemoteService.basicTypes(12, 123, true, 123.4f, 123.45, "服务端你好，我是客户端");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            iRemoteService = null;
        }
    };
}